package com.jen.queue;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DelayedMessageQueueOtherConfig
 * @Description 延时队列配置（另一种方式使用）
 * @Date 2021/12/17 15:38
 * @Author Rick Jen
 */
@Configuration
@Slf4j
public class DelayedMessageQueueOtherConfig {

    /**
     * 逾期延时队列
     */
    public final static String DELAYED_OTHER_QUEUE = "delayed_other_queue";

    /**
     * 延时交换机
     */
    public final static String DELAYED_OTHER_EXCHANGE = "delayed_other_exchange";


    /**
     * 延时路由key
     */
    public final static String DELAYED_OTHER_ROUTING_KEY = "delayed_other_routing_key";

    /**
     * 延时队列交换机
     * 注意这里的交换机类型：CustomExchange
     */
    @Bean
    public CustomExchange delayOtherExchange(){
        log.info("other延时队列交换机创建：{}",DELAYED_OTHER_EXCHANGE);
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_OTHER_EXCHANGE,"x-delayed-message",true, false,args);
    }

    /**
     * 延时队列
     */
    @Bean
    public Queue delayOtherQueue(){
        log.info("other延时队列创建：{}",DELAYED_OTHER_QUEUE);
        return new Queue(DELAYED_OTHER_QUEUE,true);
    }

    /**
     * 给延时队列绑定交换机
     */
    @Bean
    public Binding otherBindingQueue(){
        return BindingBuilder.bind(delayOtherQueue()).to(delayOtherExchange()).with(DELAYED_OTHER_ROUTING_KEY).noargs();
    }

}
